package com.liaoyegui.springboot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liaoyegui.springboot.common.Constants;
import com.liaoyegui.springboot.controller.dto.UsersDTO;
import com.liaoyegui.springboot.entity.Users;
import com.liaoyegui.springboot.exception.ServiceException;
import com.liaoyegui.springboot.mapper.UsersMapper;
import com.liaoyegui.springboot.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liaoyegui.springboot.utils.TokenUtils;
import org.springframework.stereotype.Service;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Override
    public UsersDTO UsersLogin(UsersDTO usersDTO) {
        Users one = getUsersInfo(usersDTO);
        if (one != null) {
            BeanUtil.copyProperties(one, usersDTO, true);
            return usersDTO;
        } else {
            throw new ServiceException(Constants.CODE_600, "用户名或密码错误");
        }

    }

    @Override
    public Users UsersRegister(UsersDTO usersDTO) {
        Users one = getUsersInfo(usersDTO);
        if (one == null) {
            one = new Users();
            BeanUtil.copyProperties(usersDTO, one, true);
            save(one);
        } else {
            throw new ServiceException(Constants.CODE_600, "用户名以存在");
        }
        return one;
    }


    private Users getUsersInfo(UsersDTO usersDTO) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", usersDTO.getUsername());
        queryWrapper.eq("password", usersDTO.getPassword());
        Users one;
        try {
            one = getOne(queryWrapper);//从数据库查询用户信息
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return one;
    }
}
